<?php

class tplQrecent extends StudyBuddy\Template\Template {

    protected static function func(&$a) {

        if (!empty($a['a_closed'])) {
            $a['closed'] = ' closed';
        }

        if (!empty($a['a_latest'])) {
            $reltime = \StudyBuddy\TimeAgo::format(new \DateTime($a['a_latest'][0]['t']));
            $a['last_poster'] = '<div class="lastposter fl cb">' . $a['latest_by'] . ': ' . $a['a_latest'][0]['u'] . '<br>
			<span title="' . $a['a_latest'][0]['t'] . '" class="ts">' . $reltime . '</span></div>';
        }
        if (!empty($a['hts'])) {
            $a['hts_ago'] = \StudyBuddy\TimeAgo::format(new \DateTime($a['hts']));
        }
        if (!empty($a['tags_html'])) {
            $a['custom_tags_html'] = str_replace('%1$s', 'boxr-mid-ct', $a['tags_html']);
        }
        if (isset($a['i_sel_ans']) && !empty($a['i_sel_ans'])) {
            $a['has_accept_ans'] = '-last';
        }
        if (!empty($a['b'])) {
            preg_match_all('/<img[^>]+>/i', $a['b'], $result);
            if (!empty($result[0][0])) {
                $img = str_replace('_m_h_s_d', '_s_h_s_d', $result[0][0]);
                if (strpos($img, '/img/uploaded/')) {
                    $a['imagepreview'] = '<span class="imagepreview">' . $img . '</span>';
                }
            }
            if (!empty($result[0][1])) {
                $img = str_replace('_m_h_s_d', '_s_h_s_d', $result[0][1]);
                if (strpos($img, '/img/uploaded/')) {
                    $a['imagepreview'] .= '<span class="imagepreview">' . $img . '</span>';
                }
            }
        }
        if (empty($a['dif'])) {
            $a['dif'] = '1';
        }
        
        $qid = $a['_id'];
               
        $aVars = array(
            'id' => $qid,
            'icon' => 'cplus',
            'label' => 'Follow this question',
            'class' => 'follow',
            'type' => 'q',
            'title' => 'Follow this question to be notified of new answers, comments and edits'
        );

        $oRegistry = \StudyBuddy\Registry::getInstance();
        
        if (!empty($a['a_flwrs']) && in_array($oRegistry->Viewer->getUid(), $a['a_flwrs'])) {
            $aVars['label'] = 'Following';
            $aVars['class'] = 'following';
            $aVars['icon']  = 'check';
            $aVars['title'] = 'You are following this question';
            $a['followhtml'] = '<div class=""><div class="follow_wrap">'.tplFollowButton::parse($aVars, false).'</div></div>';
        }else{
            $a['followhtml'] = '<div class=""><div class="follow_wrap">'.tplFollowButton::parse($aVars, false).'</div></div>';
        }
 
    }

    protected static $vars = array(
        '_id' => '0', //1
        'i_votes' => '0', //2
        'i_ans' => '0', //3
        'i_views' => '0', //4
        'url' => '', //5
        'intro' => '', //6
        'title' => '', //7
        'tags_c' => '', //8
        'tags_html' => '', //9
        'status' => 'un', //10
        'username' => '', //11
        'avtr' => '/images/avatar.png', //12
        'hts' => '', //13
        'i_ts' => '', //14
        'vw_s' => 's', //15
        'v_s' => '', //16
        'ans_s' => '', //17
        'deleted' => '', //18
        'closed' => '', //19
        'asked' => 'asked', //20
        'i_sticky' => '', //21
        'dot' => '', //22
        'last_poster' => '', //23
        'i_etag' => '0', //24
        'following_tag' => '', //25
        'following_q' => '', //26
        'toggle' => '', // 27
        'dif' => '1', //28
        'hts_ago' => '', //29
        'imagepreview' => '', //30
        'custom_tags_html' => '', //31
        'has_accept_ans' => '', //32
        'followhtml' => '', //33
    );
    protected static $tpl = '
        <div class="box%32$s">
            <div class="box-left">
                <table class="tbl-boxl" cellpadding="0" cellspacing="0">	                                        
                    <tr>
                        <td class="boxl-tdn">%3$s</td>
                        <td class="boxl-tdn">%4$s</td>
                    </tr>	     
                    <tr>
                        <td class="boxl-tdt">answer%17$s</td>
                        <td class="boxl-tdt">view%15$s</td>
                    </tr>	
                    <tr>
                        <td align="center">
                            %33$s
                        </td>
                        <td style="padding-top:3px;" align="center">
                            <a href="JavaScript:void(0)"><img alt="" src="/images/difico/%28$s.png" width="25px" height="20px" /></a>
                        </td>
                    </tr>
                </table>
            </div>
            <div class="box-right">
				<div class="fl"><a href="/q%1$s/%5$s" class="ql%19$s pri%21$s">%7$s</a></div>
                <div class="boxr-top">
                    <p>%6$s</p>
                </div>
                    <div class="boxr-mid preview">
                    %30$s
                </div>
                <div class="boxr-mid">
                    %31$s
                </div>
                <div class="boxr-bot">
                    <div class="boxr-bot-img">
                        <a href="JavaScript:void(0)"><img alt="" src="%12$s" width="26" height="26" /></a>
                    </div>
                    <div class="boxr-bot-txt">
                        asked by %11$s %29$s
                    </div>
                    <div class="boxr-bot-btn">
                        <a href="/q%1$s/%5$s" class="answerbutton"><span class="boxr-btn" style="border:none; background-color:transparent"></span></a>
                    </div>
                </div>
            </div>
        </div>';

}
